home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / zope / interface / interface.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  24.3 KB  |  888 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Interface object implementation
  5.  
  6. $Id: interface.py 27081 2004-08-12 19:56:31Z srichter $
  7. '''
  8. from __future__ import generators
  9. import sys
  10. import warnings
  11. import weakref
  12. from types import FunctionType
  13. from ro import ro
  14. from zope.interface.exceptions import Invalid
  15. CO_VARARGS = 4
  16. CO_VARKEYWORDS = 8
  17. TAGGED_DATA = '__interface_tagged_values__'
  18.  
  19. def invariant(call):
  20.     f_locals = sys._getframe(1).f_locals
  21.     tags = f_locals.get(TAGGED_DATA)
  22.     if tags is None:
  23.         tags = f_locals[TAGGED_DATA] = { }
  24.     
  25.     invariants = tags.get('invariants')
  26.     if invariants is None:
  27.         invariants = tags['invariants'] = []
  28.     
  29.     invariants.append(call)
  30.  
  31.  
  32. class Element(object):
  33.     
  34.     def __init__(self, __name__, __doc__ = ''):
  35.         """Create an 'attribute' description
  36.         """
  37.         if not __doc__ and __name__.find(' ') >= 0:
  38.             __doc__ = __name__
  39.             __name__ = None
  40.         
  41.         self.__name__ = __name__
  42.         self.__doc__ = __doc__
  43.         self._Element__tagged_values = { }
  44.  
  45.     
  46.     def getName(self):
  47.         ''' Returns the name of the object. '''
  48.         return self.__name__
  49.  
  50.     
  51.     def getDoc(self):
  52.         ''' Returns the documentation for the object. '''
  53.         return self.__doc__
  54.  
  55.     
  56.     def getTaggedValue(self, tag):
  57.         """ Returns the value associated with 'tag'. """
  58.         return self._Element__tagged_values[tag]
  59.  
  60.     
  61.     def queryTaggedValue(self, tag, default = None):
  62.         """ Returns the value associated with 'tag'. """
  63.         return self._Element__tagged_values.get(tag, default)
  64.  
  65.     
  66.     def getTaggedValueTags(self):
  67.         ''' Returns a list of all tags. '''
  68.         return self._Element__tagged_values.keys()
  69.  
  70.     
  71.     def setTaggedValue(self, tag, value):
  72.         """ Associates 'value' with 'key'. """
  73.         self._Element__tagged_values[tag] = value
  74.  
  75.  
  76.  
  77. class SpecificationBasePy(object):
  78.     
  79.     def providedBy(self, ob):
  80.         '''Is the interface implemented by an object
  81.  
  82.           >>> from zope.interface import *
  83.           >>> class I1(Interface):
  84.           ...     pass
  85.           >>> class C(object):
  86.           ...     implements(I1)
  87.           >>> c = C()
  88.           >>> class X(object):
  89.           ...     pass
  90.           >>> x = X()
  91.           >>> I1.providedBy(x)
  92.           False
  93.           >>> I1.providedBy(C)
  94.           False
  95.           >>> I1.providedBy(c)
  96.           True
  97.           >>> directlyProvides(x, I1)
  98.           >>> I1.providedBy(x)
  99.           True
  100.           >>> directlyProvides(C, I1)
  101.           >>> I1.providedBy(C)
  102.           True
  103.         
  104.         '''
  105.         spec = providedBy(ob)
  106.         return self in spec._implied
  107.  
  108.     
  109.     def implementedBy(self, cls):
  110.         '''Do instances of the given class implement the interface?'''
  111.         spec = implementedBy(cls)
  112.         return self in spec._implied
  113.  
  114.     
  115.     def isOrExtends(self, interface):
  116.         '''Is the interface the same as or extend the given interface
  117.  
  118.         Examples::
  119.  
  120.           >>> from zope.interface import Interface
  121.           >>> from zope.interface.declarations import Declaration
  122.           >>> class I1(Interface): pass
  123.           ...
  124.           >>> class I2(I1): pass
  125.           ...
  126.           >>> class I3(Interface): pass
  127.           ...
  128.           >>> class I4(I3): pass
  129.           ...
  130.           >>> spec = Declaration()
  131.           >>> int(spec.extends(Interface))
  132.           0
  133.           >>> spec = Declaration(I2)
  134.           >>> int(spec.extends(Interface))
  135.           1
  136.           >>> int(spec.extends(I1))
  137.           1
  138.           >>> int(spec.extends(I2))
  139.           1
  140.           >>> int(spec.extends(I3))
  141.           0
  142.           >>> int(spec.extends(I4))
  143.           0
  144.  
  145.         '''
  146.         return interface in self._implied
  147.  
  148.  
  149. SpecificationBase = SpecificationBasePy
  150.  
  151. try:
  152.     from _zope_interface_coptimizations import SpecificationBase
  153. except ImportError:
  154.     pass
  155.  
  156.  
  157. class Specification(SpecificationBase):
  158.     """Specifications
  159.  
  160.     An interface specification is used to track interface declarations
  161.     and component registrations.
  162.  
  163.     This class is a base class for both interfaces themselves and for
  164.     interface specifications (declarations).
  165.  
  166.     Specifications are mutable.  If you reassign their cases, their
  167.     relations with other specifications are adjusted accordingly.
  168.  
  169.     For example:
  170.  
  171.     >>> from zope.interface import Interface
  172.     >>> class I1(Interface):
  173.     ...     pass
  174.     >>> class I2(I1):
  175.     ...     pass
  176.     >>> class I3(I2):
  177.     ...     pass
  178.  
  179.     >>> [i.__name__ for i in I1.__bases__]
  180.     ['Interface']
  181.  
  182.     >>> [i.__name__ for i in I2.__bases__]
  183.     ['I1']
  184.  
  185.     >>> I3.extends(I1)
  186.     1
  187.  
  188.     >>> I2.__bases__ = (Interface, )
  189.  
  190.     >>> [i.__name__ for i in I2.__bases__]
  191.     ['Interface']
  192.  
  193.     >>> I3.extends(I1)
  194.     0
  195.         
  196.     """
  197.     isOrExtends = SpecificationBase.isOrExtends
  198.     providedBy = SpecificationBase.providedBy
  199.     
  200.     def isImplementedByInstancesOf(self, cls):
  201.         warnings.warn('isImplementedByInstancesOf has been renamed to implementedBy', DeprecationWarning, stacklevel = 2)
  202.         return self.implementedBy(cls)
  203.  
  204.     
  205.     def isImplementedBy(self, ob):
  206.         warnings.warn('isImplementedBy has been renamed to providedBy', DeprecationWarning, stacklevel = 2)
  207.         return self.providedBy(ob)
  208.  
  209.     
  210.     def __init__(self, bases = ()):
  211.         self._implied = { }
  212.         self.dependents = weakref.WeakKeyDictionary()
  213.         self.__bases__ = tuple(bases)
  214.  
  215.     
  216.     def subscribe(self, dependent):
  217.         self.dependents[dependent] = 1
  218.  
  219.     
  220.     def unsubscribe(self, dependent):
  221.         del self.dependents[dependent]
  222.  
  223.     
  224.     def __setBases(self, bases):
  225.         for b in self.__bases__:
  226.             b.unsubscribe(self)
  227.         
  228.         self.__dict__['__bases__'] = bases
  229.         for b in bases:
  230.             b.subscribe(self)
  231.         
  232.         self.changed()
  233.  
  234.     __bases__ = property((lambda self: self.__dict__.get('__bases__', ())), __setBases)
  235.     
  236.     def changed(self):
  237.         '''We, or something we depend on, have changed
  238.         '''
  239.         implied = self._implied
  240.         implied.clear()
  241.         ancestors = ro(self)
  242.         self.__sro__ = tuple(ancestors)
  243.         self.__iro__ = [](_[1])
  244.         for ancestor in ancestors:
  245.             implied[ancestor] = ()
  246.         
  247.         for dependent in self.dependents.keys():
  248.             dependent.changed()
  249.         
  250.  
  251.     
  252.     def interfaces(self):
  253.         """Return an iterator for the interfaces in the specification
  254.  
  255.         for example::
  256.  
  257.           >>> from zope.interface import Interface
  258.           >>> class I1(Interface): pass
  259.           ...
  260.           >>> class I2(I1): pass
  261.           ...
  262.           >>> class I3(Interface): pass
  263.           ...
  264.           >>> class I4(I3): pass
  265.           ...
  266.           >>> spec = Specification((I2, I3))
  267.           >>> spec = Specification((I4, spec))
  268.           >>> i = spec.interfaces()
  269.           >>> i.next().getName()
  270.           'I4'
  271.           >>> i.next().getName()
  272.           'I2'
  273.           >>> i.next().getName()
  274.           'I3'
  275.           >>> list(i)
  276.           []
  277.         """
  278.         seen = { }
  279.         for base in self.__bases__:
  280.             for interface in base.interfaces():
  281.                 if interface not in seen:
  282.                     seen[interface] = 1
  283.                     yield interface
  284.                     continue
  285.             
  286.         
  287.  
  288.     
  289.     def extends(self, interface, strict = True):
  290.         '''Does the specification extend the given interface?
  291.  
  292.         Test whether an interface in the specification extends the
  293.         given interface
  294.  
  295.         Examples::
  296.  
  297.           >>> from zope.interface import Interface
  298.           >>> from zope.interface.declarations import Declaration
  299.           >>> class I1(Interface): pass
  300.           ...
  301.           >>> class I2(I1): pass
  302.           ...
  303.           >>> class I3(Interface): pass
  304.           ...
  305.           >>> class I4(I3): pass
  306.           ...
  307.           >>> spec = Declaration()
  308.           >>> int(spec.extends(Interface))
  309.           0
  310.           >>> spec = Declaration(I2)
  311.           >>> int(spec.extends(Interface))
  312.           1
  313.           >>> int(spec.extends(I1))
  314.           1
  315.           >>> int(spec.extends(I2))
  316.           1
  317.           >>> int(spec.extends(I3))
  318.           0
  319.           >>> int(spec.extends(I4))
  320.           0
  321.           >>> I2.extends(I2)
  322.           0
  323.           >>> I2.extends(I2, False)
  324.           1
  325.           >>> I2.extends(I2, strict=False)
  326.           1
  327.  
  328.         '''
  329.         if not interface in self._implied and not strict:
  330.             pass
  331.         return self != interface
  332.  
  333.     
  334.     def weakref(self, callback = None):
  335.         if callback is None:
  336.             return weakref.ref(self)
  337.         else:
  338.             return weakref.ref(self, callback)
  339.  
  340.  
  341.  
  342. class InterfaceClass(Element, Specification):
  343.     '''Prototype (scarecrow) Interfaces Implementation.'''
  344.     
  345.     def __init__(self, name, bases = (), attrs = None, __doc__ = None, __module__ = None):
  346.         if __module__ is None:
  347.             pass
  348.         None if attrs is not None and '__module__' in attrs and isinstance(attrs['__module__'], str) else None<EXCEPTION MATCH>(AttributeError, KeyError)
  349.         self.__module__ = __module__
  350.         if attrs is None:
  351.             attrs = { }
  352.         
  353.         d = attrs.get('__doc__')
  354.         if d is not None:
  355.             if not isinstance(d, Attribute):
  356.                 if __doc__ is None:
  357.                     __doc__ = d
  358.                 
  359.                 del attrs['__doc__']
  360.             
  361.         
  362.         if __doc__ is None:
  363.             __doc__ = ''
  364.         
  365.         Element.__init__(self, name, __doc__)
  366.         if attrs.has_key(TAGGED_DATA):
  367.             tagged_data = attrs[TAGGED_DATA]
  368.             del attrs[TAGGED_DATA]
  369.         else:
  370.             tagged_data = None
  371.         if tagged_data is not None:
  372.             for key, val in tagged_data.items():
  373.                 self.setTaggedValue(key, val)
  374.             
  375.         
  376.         for b in bases:
  377.             if not isinstance(b, InterfaceClass):
  378.                 raise TypeError, 'Expected base interfaces'
  379.                 continue
  380.         
  381.         Specification.__init__(self, bases)
  382.         for k, v in attrs.items():
  383.             if isinstance(v, Attribute):
  384.                 v.interface = name
  385.                 if not v.__name__:
  386.                     v.__name__ = k
  387.                 
  388.             v.__name__
  389.             if isinstance(v, FunctionType):
  390.                 attrs[k] = fromFunction(v, name, name = k)
  391.                 continue
  392.             raise InvalidInterface('Concrete attribute, %s' % k)
  393.         
  394.         self._InterfaceClass__attrs = attrs
  395.         self.__identifier__ = '%s.%s' % (self.__module__, self.__name__)
  396.  
  397.     
  398.     def interfaces(self):
  399.         """Return an iterator for the interfaces in the specification
  400.  
  401.         for example::
  402.  
  403.           >>> from zope.interface import Interface
  404.           >>> class I1(Interface): pass
  405.           ...
  406.           >>> 
  407.           >>> i = I1.interfaces()
  408.           >>> i.next().getName()
  409.           'I1'
  410.           >>> list(i)
  411.           []
  412.         """
  413.         yield self
  414.  
  415.     
  416.     def getBases(self):
  417.         return self.__bases__
  418.  
  419.     
  420.     def isEqualOrExtendedBy(self, other):
  421.         '''Same interface or extends?'''
  422.         if self == other:
  423.             return True
  424.         
  425.         return other.extends(self)
  426.  
  427.     
  428.     def names(self, all = False):
  429.         '''Return the attribute names defined by the interface.'''
  430.         if not all:
  431.             return self._InterfaceClass__attrs.keys()
  432.         
  433.         r = { }
  434.         for name in self._InterfaceClass__attrs.keys():
  435.             r[name] = 1
  436.         
  437.         for base in self.__bases__:
  438.             for name in base.names(all):
  439.                 r[name] = 1
  440.             
  441.         
  442.         return r.keys()
  443.  
  444.     
  445.     def __iter__(self):
  446.         return iter(self.names(all = True))
  447.  
  448.     
  449.     def namesAndDescriptions(self, all = False):
  450.         '''Return attribute names and descriptions defined by interface.'''
  451.         if not all:
  452.             return self._InterfaceClass__attrs.items()
  453.         
  454.         r = { }
  455.         for name, d in self._InterfaceClass__attrs.items():
  456.             r[name] = d
  457.         
  458.         for base in self.__bases__:
  459.             for name, d in base.namesAndDescriptions(all):
  460.                 if name not in r:
  461.                     r[name] = d
  462.                     continue
  463.             
  464.         
  465.         return r.items()
  466.  
  467.     
  468.     def getDescriptionFor(self, name):
  469.         '''Return the attribute description for the given name.'''
  470.         r = self.queryDescriptionFor(name)
  471.         if r is not None:
  472.             return r
  473.         
  474.         raise KeyError, name
  475.  
  476.     __getitem__ = getDescriptionFor
  477.     
  478.     def __contains__(self, name):
  479.         return self.queryDescriptionFor(name) is not None
  480.  
  481.     
  482.     def queryDescriptionFor(self, name, default = None):
  483.         '''Return the attribute description for the given name.'''
  484.         r = self._InterfaceClass__attrs.get(name, self)
  485.         if r is not self:
  486.             return r
  487.         
  488.         for base in self.__bases__:
  489.             r = base.queryDescriptionFor(name, self)
  490.             if r is not self:
  491.                 return r
  492.                 continue
  493.         
  494.         return default
  495.  
  496.     get = queryDescriptionFor
  497.     
  498.     def deferred(self):
  499.         '''Return a defered class corresponding to the interface.'''
  500.         if hasattr(self, '_deferred'):
  501.             return self._deferred
  502.         
  503.         klass = { }
  504.         exec 'class %s: pass' % self.__name__ in klass
  505.         klass = klass[self.__name__]
  506.         self._InterfaceClass__d(klass.__dict__)
  507.         self._deferred = klass
  508.         return klass
  509.  
  510.     
  511.     def validateInvariants(self, obj, errors = None):
  512.         '''validate object to defined invariants.'''
  513.         for call in self.queryTaggedValue('invariants', []):
  514.             
  515.             try:
  516.                 call(obj)
  517.             continue
  518.             except Invalid:
  519.                 e = None
  520.                 if errors is None:
  521.                     raise 
  522.                 else:
  523.                     errors.append(e)
  524.                 errors is None
  525.             
  526.  
  527.         
  528.         for base in self.__bases__:
  529.             
  530.             try:
  531.                 base.validateInvariants(obj, errors)
  532.             continue
  533.             except Invalid:
  534.                 None<EXCEPTION MATCH>Invalid
  535.                 None<EXCEPTION MATCH>Invalid
  536.                 if errors is None:
  537.                     raise 
  538.                 
  539.                 errors is None
  540.             
  541.  
  542.         
  543.  
  544.     
  545.     def _getInterface(self, ob, name):
  546.         '''Retrieve a named interface.'''
  547.         pass
  548.  
  549.     
  550.     def __d(self, dict):
  551.         for k, v in self._InterfaceClass__attrs.items():
  552.             if isinstance(v, Method) and k not in dict:
  553.                 dict[k] = v
  554.                 continue
  555.         
  556.         for b in self.__bases__:
  557.             b._InterfaceClass__d(dict)
  558.         
  559.  
  560.     
  561.     def __repr__(self):
  562.         r = getattr(self, '_v_repr', self)
  563.         if r is self:
  564.             name = self.__name__
  565.             m = self.__module__
  566.             if m:
  567.                 name = '%s.%s' % (m, name)
  568.             
  569.             r = '<%s %s>' % (self.__class__.__name__, name)
  570.             self._v_repr = r
  571.         
  572.         return r
  573.  
  574.     
  575.     def __call__():
  576.         marker = object()
  577.         
  578.         def __call__(self, obj, alternate = marker):
  579.             """Adapt an object to the interface
  580.  
  581.                The sematics based on those of the PEP 246 adapt function.
  582.  
  583.                If an object cannot be adapted, then a TypeError is raised::
  584.  
  585.                  >>> import zope.interface
  586.                  >>> class I(zope.interface.Interface):
  587.                  ...     pass
  588.  
  589.                  >>> I(0)
  590.                  Traceback (most recent call last):
  591.                  ...
  592.                  TypeError: ('Could not adapt', 0, <InterfaceClass zope.interface.interface.I>)
  593.  
  594.                unless an alternate value is provided as a second
  595.                positional argument::
  596.  
  597.                  >>> I(0, 'bob')
  598.                  'bob'
  599.  
  600.                If an object already implements the interface, then it will be
  601.                returned::
  602.  
  603.                  >>> class C(object):
  604.                  ...     zope.interface.implements(I)
  605.  
  606.                  >>> obj = C()
  607.                  >>> I(obj) is obj
  608.                  True
  609.  
  610.                If an object implements __conform__, then it will be used::
  611.  
  612.                  >>> class C(object):
  613.                  ...     zope.interface.implements(I)
  614.                  ...     def __conform__(self, proto):
  615.                  ...          return 0
  616.  
  617.                  >>> I(C())
  618.                  0
  619.  
  620.                Adapter hooks (see __adapt__) will also be used, if present:
  621.  
  622.                  >>> from zope.interface.interface import adapter_hooks
  623.                  >>> def adapt_0_to_42(iface, obj):
  624.                  ...     if obj == 0:
  625.                  ...         return 42
  626.  
  627.                  >>> adapter_hooks.append(adapt_0_to_42)
  628.                  >>> I(0)
  629.                  42
  630.  
  631.                  >>> adapter_hooks.remove(adapt_0_to_42)
  632.                  >>> I(0)
  633.                  Traceback (most recent call last):
  634.                  ...
  635.                  TypeError: ('Could not adapt', 0, <InterfaceClass zope.interface.interface.I>)
  636.  
  637.             """
  638.             conform = getattr(obj, '__conform__', None)
  639.             if conform is not None:
  640.                 
  641.                 try:
  642.                     adapter = conform(self)
  643.                 except TypeError:
  644.                     if sys.exc_info()[2].tb_next is not None:
  645.                         raise 
  646.                     
  647.                 except:
  648.                     sys.exc_info()[2].tb_next is not None
  649.  
  650.                 if adapter is not None:
  651.                     return adapter
  652.                 
  653.             
  654.             adapter = self.__adapt__(obj)
  655.             if adapter is None:
  656.                 if alternate is not marker:
  657.                     return alternate
  658.                 
  659.                 raise TypeError('Could not adapt', obj, self)
  660.             
  661.             return adapter
  662.  
  663.         return __call__
  664.  
  665.     __call__ = __call__()
  666.     
  667.     def __adapt__(self, obj):
  668.         """Adapt an object to the reciever
  669.  
  670.            This method is normally not called directly. It is called by
  671.            the PEP 246 adapt framework and by the interface __call__
  672.            operator. 
  673.  
  674.            The adapt method is responsible for adapting an object to
  675.            the reciever.
  676.  
  677.            The default version returns None::
  678.  
  679.              >>> import zope.interface
  680.              >>> class I(zope.interface.Interface):
  681.              ...     pass
  682.  
  683.              >>> I.__adapt__(0)
  684.  
  685.            unless the object given provides the interface::
  686.  
  687.              >>> class C(object):
  688.              ...     zope.interface.implements(I)
  689.  
  690.              >>> obj = C()
  691.              >>> I.__adapt__(obj) is obj
  692.              True
  693.  
  694.            Adapter hooks can be provided (or removed) to provide custom
  695.            adaptation. We'll install a silly hook that adapts 0 to 42.
  696.            We install a hook by simply adding it to the adapter_hooks
  697.            list::
  698.  
  699.              >>> from zope.interface.interface import adapter_hooks
  700.              >>> def adapt_0_to_42(iface, obj):
  701.              ...     if obj == 0:
  702.              ...         return 42
  703.  
  704.              >>> adapter_hooks.append(adapt_0_to_42)
  705.              >>> I.__adapt__(0)
  706.              42
  707.  
  708.            Hooks must either return an adapter, or None if no adapter can
  709.            be found.
  710.  
  711.            Hooks can be uninstalled by removing them from the list::
  712.  
  713.              >>> adapter_hooks.remove(adapt_0_to_42)
  714.              >>> I.__adapt__(0)
  715.  
  716.            """
  717.         if self.providedBy(obj):
  718.             return obj
  719.         
  720.         for hook in adapter_hooks:
  721.             adapter = hook(self, obj)
  722.             if adapter is not None:
  723.                 return adapter
  724.                 continue
  725.         
  726.  
  727.     
  728.     def __reduce__(self):
  729.         return self.__name__
  730.  
  731.     
  732.     def __cmp(self, o1, o2):
  733.         """Make interfaces sortable
  734.  
  735.         TODO: It would ne nice if:
  736.  
  737.            More specific interfaces should sort before less specific ones.
  738.            Otherwise, sort on name and module.
  739.  
  740.            But this is too complicated, and we're going to punt on it
  741.            for now.
  742.  
  743.         For now, sort on interface and module name.
  744.  
  745.         None is treated as a pseudo interface that implies the loosest
  746.         contact possible, no contract. For that reason, all interfaces
  747.         sort before None.
  748.  
  749.         """
  750.         if o1 == o2:
  751.             return 0
  752.         
  753.         if o1 is None:
  754.             return 1
  755.         
  756.         if o2 is None:
  757.             return -1
  758.         
  759.         n1 = (getattr(o1, '__name__', ''), getattr(getattr(o1, '__module__', None), '__name__', ''))
  760.         n2 = (getattr(o2, '__name__', ''), getattr(getattr(o2, '__module__', None), '__name__', ''))
  761.         return cmp(n1, n2)
  762.  
  763.     
  764.     def __lt__(self, other):
  765.         c = self._InterfaceClass__cmp(self, other)
  766.         return c < 0
  767.  
  768.     
  769.     def __gt__(self, other):
  770.         c = self._InterfaceClass__cmp(self, other)
  771.         return c > 0
  772.  
  773.  
  774. adapter_hooks = []
  775. Interface = InterfaceClass('Interface', __module__ = 'zope.interface')
  776.  
  777. class Attribute(Element):
  778.     '''Attribute descriptions
  779.     '''
  780.     pass
  781.  
  782.  
  783. class Method(Attribute):
  784.     '''Method interfaces
  785.  
  786.     The idea here is that you have objects that describe methods.
  787.     This provides an opportunity for rich meta-data.
  788.     '''
  789.     interface = ''
  790.     
  791.     def __call__(self, *args, **kw):
  792.         raise BrokenImplementation(self.interface, self.__name__)
  793.  
  794.     
  795.     def getSignatureInfo(self):
  796.         return {
  797.             'positional': self.positional,
  798.             'required': self.required,
  799.             'optional': self.optional,
  800.             'varargs': self.varargs,
  801.             'kwargs': self.kwargs }
  802.  
  803.     
  804.     def getSignatureString(self):
  805.         sig = '('
  806.         for v in self.positional:
  807.             sig = sig + v
  808.             if v in self.optional.keys():
  809.                 sig = sig + '=%s' % `self.optional[v]`
  810.             
  811.             sig = sig + ', '
  812.         
  813.         if self.varargs:
  814.             sig = sig + '*%s, ' % self.varargs
  815.         
  816.         if self.kwargs:
  817.             sig = sig + '**%s, ' % self.kwargs
  818.         
  819.         if self.positional and self.varargs or self.kwargs:
  820.             sig = sig[:-2]
  821.         
  822.         sig = sig + ')'
  823.         return sig
  824.  
  825.  
  826.  
  827. def fromFunction(func, interface = '', imlevel = 0, name = None):
  828.     if not name:
  829.         pass
  830.     name = func.__name__
  831.     m = Method(name, func.__doc__)
  832.     if not func.func_defaults:
  833.         pass
  834.     defaults = ()
  835.     c = func.func_code
  836.     na = c.co_argcount - imlevel
  837.     names = c.co_varnames[imlevel:]
  838.     d = { }
  839.     nr = na - len(defaults)
  840.     if nr < 0:
  841.         defaults = defaults[-nr:]
  842.         nr = 0
  843.     
  844.     for i in range(len(defaults)):
  845.         d[names[i + nr]] = defaults[i]
  846.     
  847.     m.positional = names[:na]
  848.     m.required = names[:nr]
  849.     m.optional = d
  850.     argno = na
  851.     if c.co_flags & CO_VARARGS:
  852.         m.varargs = names[argno]
  853.         argno = argno + 1
  854.     else:
  855.         m.varargs = None
  856.     if c.co_flags & CO_VARKEYWORDS:
  857.         m.kwargs = names[argno]
  858.     else:
  859.         m.kwargs = None
  860.     m.interface = interface
  861.     for k, v in func.__dict__.items():
  862.         m.setTaggedValue(k, v)
  863.     
  864.     return m
  865.  
  866.  
  867. def fromMethod(meth, interface = ''):
  868.     func = meth.im_func
  869.     return fromFunction(func, interface, imlevel = 1)
  870.  
  871.  
  872. def _wire():
  873.     classImplements = classImplements
  874.     import zope.interface.declarations
  875.     IAttribute = IAttribute
  876.     import zope.interface.interfaces
  877.     classImplements(Attribute, IAttribute)
  878.     IMethod = IMethod
  879.     import zope.interface.interfaces
  880.     classImplements(Method, IMethod)
  881.     IInterface = IInterface
  882.     import zope.interface.interfaces
  883.     classImplements(InterfaceClass, IInterface)
  884.  
  885. from zope.interface.declarations import providedBy, implementedBy
  886. from zope.interface.exceptions import InvalidInterface
  887. from zope.interface.exceptions import BrokenImplementation
  888.